package com.hy.medicine.vo;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.hy.medicine.MedicineBasicInfo;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 药品库存信息展示对象
 */
@Data
public class MedicineStockVO implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 库存记录ID
     */
    private Integer stockId;

    /**
     * 药品ID
     */
    private Integer medicineId;

    /**
     * 药品名称
     */
    private String medicineName;

    /**
     * 规格
     */
    private String specification;

    /**
     * 生产厂家
     */
    private String manufacturer;

    /**
     * 库存数量
     */
    private Integer minStock;

    /**
     * 批准文号
     */
    private String approvalNumber;



    /**
     * 单价
     */
    private BigDecimal price;

    /**
     * 入库时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date inDate;

    /**
     * 最近出库时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date outDate;

    /**
     * 存放位置
     */
    private String warehouseLocation;

    /**
     * 通用分类名称
     */
    private String commonCategoryName;

    /**
     * 处方分类名称
     */
    private String prescriptionCategoryName;

    /**
     * 保质期(月)
     */
    private Integer shelfLife;

    /**
     * 库存金额（库存数量 * 单价）
     */
    public BigDecimal getStockAmount() {
        if (minStock != null && price != null) {
            return price.multiply(new BigDecimal(minStock));
        }
        return BigDecimal.ZERO;
    }

    /**
     * 是否库存预警
     */
    private Boolean isWarning;

    @TableField(exist = false)
    private MedicineBasicInfo medicine; // 关联的药品信息

    /**
     * 是否即将过期
     */
    private Boolean isExpiring;

    /**
     * 距离过期天数
     */
    private Integer daysToExpire;

    /**
     * 预警原因
     */
    private String warningReason;

    /**
     * 计算距离过期天数并设置相关状态
     */
    public void calculateDaysToExpire() {
        if (inDate != null && shelfLife != null) {
            Date expiryDate = new Date(inDate.getTime() + (long)shelfLife * 30 * 24 * 60 * 60 * 1000);
            long diff = expiryDate.getTime() - System.currentTimeMillis();
            this.daysToExpire = (int)(diff / (24 * 60 * 60 * 1000));
            this.isExpiring = this.daysToExpire <= 90;

            // 设置预警原因
            if (this.isExpiring) {
                this.warningReason = this.warningReason == null ?
                    "即将过期" : this.warningReason + "，即将过期";
            }
        }
    }
}
